package com.ztewatch.wearcontacts.utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PaintDrawable;
import android.net.Uri;

import com.ztewatch.wearcontacts.R;

public class CallUtil {
	private static Canvas sCanvas = new Canvas();
	private static final Rect sOldBounds = new Rect();

	public static Intent getCallIntent(String number) {
		Uri uri = null;
		if (isUriNumber(number)) {
			uri = Uri.fromParts("sip", number, null);
		} else {
			uri = Uri.fromParts("tel", number, null);
		}
		Intent intent = new Intent(Intent.ACTION_CALL, uri);
		return intent;
	}

	public static boolean isUriNumber(String number) {
		return number != null
				&& (number.contains("@") || number.contains("%40"));
	}

    public static Uri getCallUri(String number) {
        if (isUriNumber(number)) {
             return Uri.fromParts("sip", number, null);
        }
        return Uri.fromParts("tel", number, null);
     }
    
    public static Intent getVideoCallIntent(String number) {
    	Uri uri = getCallUri(number);
        final Intent intent = new Intent(Intent.ACTION_CALL, uri);
//        intent.putExtra("is_default_dialer", true);
        intent.putExtra("android.telecom.extra.START_CALL_WITH_VIDEO_STATE", 3);
        return intent;
    }
    
	public static Bitmap createIconBitmap(Drawable icon, Context context) {
		synchronized (sCanvas) {
			int width = 144;
			int height = 144;
			if (icon instanceof PaintDrawable) {
				PaintDrawable painter = (PaintDrawable) icon;
				painter.setIntrinsicWidth(width);
				painter.setIntrinsicHeight(height);
			} else if (icon instanceof BitmapDrawable) {
				// Ensure the bitmap has a density.
				BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
				Bitmap bitmap = bitmapDrawable.getBitmap();
				if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
					bitmapDrawable.setTargetDensity(context.getResources()
							.getDisplayMetrics());
				}
			}
			int sourceWidth = icon.getIntrinsicWidth();
			int sourceHeight = icon.getIntrinsicHeight();
			if (sourceWidth > 0 && sourceHeight > 0) {
				// Scale the icon proportionally to the icon dimensions
				final float ratio = (float) sourceWidth / sourceHeight;
				if (sourceWidth > sourceHeight) {
					height = (int) (width / ratio);
				} else if (sourceHeight > sourceWidth) {
					width = (int) (height * ratio);
				}
			}
			// no intrinsic size --> use default size
			int textureWidth = 144;
			int textureHeight = 144;

			final Bitmap bitmap = Bitmap.createBitmap(textureWidth,
					textureHeight, Bitmap.Config.ARGB_8888);
			final Canvas canvas = sCanvas;
			canvas.setBitmap(bitmap);

			final int left = (textureWidth - width) / 2;
			final int top = (textureHeight - height) / 2;

			sOldBounds.set(icon.getBounds());
			icon.setBounds(left, top, left + width, top + height);
			icon.draw(canvas);
			icon.setBounds(sOldBounds);
			canvas.setBitmap(null);

			return bitmap;
		}
	}

	public static Bitmap createCornerMarkBitmap(Bitmap originalicon, int count,
			Context context) {
		synchronized (sCanvas) {
			Bitmap cornerMarkIcon = createIconBitmap(context.getResources()
					.getDrawable(R.drawable.corner_mask), context);
			final Canvas canvas = sCanvas;
			Paint paint = new Paint();
			paint.setAntiAlias(true);
			canvas.setBitmap(originalicon);

			canvas.save();
			canvas.drawBitmap(cornerMarkIcon, 0, 0, paint);

			int iconSize = (int) context.getResources().getDimension(
					android.R.dimen.app_icon_size);

			iconSize = 115;
			paint.setColor(Color.RED);
			paint.setStrokeWidth(2);
			paint.setTextSize(24);
			if (count > 99) {
				canvas.drawText("n", iconSize, 25, paint);
			} else {
				if (count > 9) {
					canvas.drawText(String.valueOf(count), iconSize - 5, 30,
							paint);
				} else {
					canvas.drawText(String.valueOf(count), iconSize, 30, paint);

				}
			}
			canvas.restore();
		}
		return originalicon;

	}
}
